#!/usr/bin/perl

# (c) 2006-2007 by J.P. Peach & Associates
# All rights reserved

# Print a basic template of the jppeach.com webpage listing all the files in a directory
# An optional suffix can be used to limit what files are listed
# Set $Filter to a value to impose the filter using grep rules
# Set @BlackList to list all files and directories that are not to be shown
# Set @WhiteList to list all files that are to be shown no matter what
#
# Set $Title as the title to use at the top of the page as a header

use File::Basename;
use URI::Escape;

###################
# Defines
###################
$true = 1;
$false = 0;

###################
# Options
###################

$Title = "Client Access";
$isParentDirTitle = $false;                    # Use the name of the parent directory as the title.

# What files/dir are to be displayed
@BlackList        = (".", "..", ".htaccess", ".htpasswd");
@WhiteList        = ();
$isSort           = $true;                    # Sort the filelist
$HideDotFiles     = $true;                    # Hide files that start with a '.'
$HideFiles        = $false;                   # Hide all files
$HideDirs         = $false;                   # Hide all directories
$HideNoPermission = $true;                    # Hide any files or directories that user has not file permission to access
$HideSelf         = $true;                    # Hide this script 
$HideFilter       = $false;                   # Hide items that do not match the grep expression in $Filter
$Filter           = "\[[:alnum:]]\$";         # grep expression of files to except. 

# Labels for links
$RelabelParentDir = $true;                    # If the parent directory ".." is displayed, then relabel it to $LabelParentDir
$LabelParentDir   = "&lt;UP&gt;";             # Label to replace ".." (HTML)
$RelabelDir       = $false;                   # if the it is a directory add $LabelDir to the end of the file.  It will not override RelabelParentDir.
$LabelDir         = "&lt;DIR&gt;";            # Label to add to dirs (HTML)
$ShowFileSize     = $true;                    # show the size of the file in bytes

# HTML Styles
$FileClass        = "FileLink";               # html class to use for a file
$DirClass         = "DirLink";                # html class to use for a dir
$LabelClass       = "bodycopy";               # html class to use for the labelextensions

###################################################################################
#                                                                                 #
# THERE IS PROBABLY NO NEED TO EDIT BELOW THIS LINE                               #
###################################################################################

# Get the name of the parent directory to use as the title for the page
if ($isParentDirTitle) {
	$tmp = `pwd`;
	chomp($tmp);
	($Title,$path,$suffix) = fileparse($tmp);
}

print "Content-type: text/html\n\n";

print << "__END__";

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML lang="en-us">
<HEAD>
	<META NAME="robots" CONTENT="index, follow">
	<META NAME="keywords" CONTENT="J.P. Peach and Associates, design, logo, solutions, creative, internet, free estimate, burlington, vermont, VT, designer, interactive, database, program, hosting, domain, style, dynamic, web hosting">
	<META NAME="description" CONTENT="Providing custom design services to businesses and individuals. We specialize in website development, graphic design, e-commerce solutions, maintenance, domain registration and hosting. Request your free estimate today.">
	<META NAME="Author" CONTENT="J.P. Peach and Associates">
	<META NAME="copywrite" CONTENT="All materials on this website is copyright 2000-2007 by J.P. Peach and Associates. All rights reserved.">

	<title>J.P. Peach &amp; Associates</title>
  	<link rel=StyleSheet HREF="http://www.jppeach.net/jppeach.css" TYPE="text/css">
</HEAD>
<BODY TOPMARGIN=0 LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0" BGCOLOR="#FFFFFF" onLoad='loaded=true;'>
<table cellpadding=0 cellspacing=0 border=0 width=100%>
	<tr bgcolor="#FD8429"> 
		<td height=5 colspan=3>
			<img src="http://www.jppeach.net/images/border.gif" alt="border" height=5>
		</td>
	</tr>
	<tr>
		<td width=5 bgcolor="#FD8429">
			<img src="http://www.jppeach.net/images/border.gif" alt="border" width=5>
		</td>
		<td>
			<img src="http://www.jppeach.net/images/header.jpg" alt="J.P. Peach & Associates Logo" width=500 height=167>
		</td>
		<td width=5 bgcolor="#FD8429">
			<img src="http://www.jppeach.net/images/border.gif" alt="border" width=5>
		</td>
	</tr>
	<tr>
		<td width=5 bgcolor="#FD8429">
			<img src="http://www.jppeach.net/images/border.gif" alt="border" width=5>
		</td>
		<td align=center>
			<font size=6>$Title</font><br>
			<br>
		</td>
		<td width=5 bgcolor="#FD8429">
			<img src="http://www.jppeach.net/images/border.gif" alt="border" width=5>
		</td>
	</tr>
	<tr>
		<td width=5 bgcolor="#FD8429">
			<img src="http://www.jppeach.net/images/border.gif" alt="border" width=5>
		</td>
		<td align=left>
			<table border=0 cellspacing=0 cellpadding=0>
__END__

# Get a list of all files and directories
opendir(DIR, ".");
@files = readdir(DIR);
closedir(DIR);

# Sort the files
if ($isSort) {
		@files = sort(@files);
}

# Step through each item and process it
foreach $file (@files) {
	
	$isValid = $true;                                                       # Set the default

	# filter out anything that is not on the filter list
	if ($HideFilter) {
		$isValid = grep(/$Filter/,$file);                               # Check to see if it is valid based on it passing the $Filter criteria
	}
	
	# Check the blacklist
	foreach (@BlackList) {
		if ($file eq $_) { $isValid = $false; }
	}

	# Check to see if dot files are to be excluded
	if ($HideDotFiles && substr($file,0, 1) eq "." && (-f $file)) { $isValid = $false; }
	
	# Check to see if it is a file (not a directory) and if it needs to be excluded
	if ($HideFiles && (-f $file)) { $isValid = $false; }
	
	# Check to see if it is a directory and if it needs to be excluded
	if ($HideDirs && (-d $file)) { $isValid = $false; }
	
	# Check to see if user has permission to access item
	if ($HideNoPermission) {
		if (!(-r $file)) { $isValid = $false; }
		if ((-d $file) && !(-x $file)) { $isValid = $false; }
	}
	
	# Check to see if this script is the file and hide it
	if ($HideSelf && $file eq basename($0)) { $isValid = $false; }
	
	# Check the whitelist, if it is on list even if it is invalid it will make it valid
	foreach (@WhiteList) {
		if ($file eq $_) { $isValid = $true; }
	}
	
	if ($isValid) {
		
		# Set the file label defaults
		$label = $file;
		$labelextension = "";
		$labelextension2 = "";
		
		if ($RelabelParentDir && $file eq "..") { $label = $LabelParentDir; }
		
		# Set the label extensions
		$labelextension = "";
		if ($RelabelDir && (-d $file)) { 
			$labelextension = "$LabelDir"; 
		}
		
		# Show the file size
		if ($ShowFileSize && (-f $file)) { 
			$labelextension = (-s $file);
			$labelextension2 = "Bytes"; 
		}
		
		# Set the anchor html class
		$class = $FileClass;												# Set the default
		if (-d $file) { $class = $DirClass; }                          # Override if it is a dir
		
		# Create a safe version of the 'file'. That is, escape any single quotes.
		$file_safe = uri_escape($file);
		$file_safe = uri_escape($file_safe, "'");
	
print << "__END__";
	<tr>
		<td>&nbsp;</td>
		<td align=left nowrap><a href='$file_safe' class=$class>$label</a></td>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
		<td align=right nowrap><font class=$LabelClass>$labelextension</font></td>
		<td>&nbsp;</td>
		<td align=left nowrap><font class=$LabelClass>$labelextension2</font></td>
	<tr>
__END__
  }
}
print "<BR>\n";

print << "__END__";
			</table>
			<br>
		</td>
		<td width=5 bgcolor="#FD8429">
			<img src="http://www.jppeach.net/images/border.gif" alt="border" width=5>
		</td>
	</tr>
	<tr bgcolor="#FD8429">
		<td height=5 colspan=3>
			<img src="http://www.jppeach.net/images/border.gif" alt="border"  height=5>
		</td>
	</tr>
</table>
</body>
</html>
__END__
